﻿using DivisonsCodeTools.Business.Configure;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DivisonsCodeTools.Frm
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }
        DivisonsCodeTools.Business.DivisonBus db = new Business.DivisonBus();
        /// <summary>
        /// 开始采集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItemBegin_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定开始采集代码吗？", "系统提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                string file = Config.GetInstance().FilePath + "\\" + Config.GetInstance().FileName;
                if (File.Exists(file))
                    File.Delete(file);


                db.form = this;
                db.richTextBox = this.richTextBox1;
                db.Start();
                this.MenuItemBegin.Enabled = false;
            }
        }

        /// <summary>
        /// 停止采集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItemStop_Click(object sender, EventArgs e)
        {
            db.Stop();
            MessageBox.Show("已经停止！");
            this.MenuItemBegin.Enabled = true;
        }

        /// <summary>
        ///  设置相关参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItemTools_Click(object sender, EventArgs e)
        {
            Frm.FrmTools ft = new FrmTools();
            ft.ShowDialog();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            if (!AppConfig.IsAppConfig())
                AppConfig.InitConfig();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            richTextBox1.Focus();
        }

        /// <summary>
        /// 设置Mariadb数据库参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItemMariadbTools_Click(object sender, EventArgs e)
        {
            FrmMariadbTools fmt = new FrmMariadbTools();
            fmt.ShowDialog();
        }

        /// <summary>
        /// 添加到数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItemMariadbInsert_Click(object sender, EventArgs e)
        {
            DivisonsCodeTools.Business.DivisonBus bus = new Business.DivisonBus();
            bool tag = bus.MariaDBSave();
            if (tag)
                MessageBox.Show("添加成功");
            else
                MessageBox.Show("添加失败");
        }
    }
}
